package com.dingup.mock.controller.writing;

import com.dingup.api.base.HttpResContent;
import com.dingup.mock.bean.exam.ExaminationInfo;
import com.dingup.mock.bean.writing.ExamWritingInfo;
import com.dingup.mock.bean.writing.TsWritingQuestion;
import com.dingup.mock.bean.writing.WritingDataMapInfo;
import com.dingup.mock.dao.writing.TsWritingQuestionsMapper;
import com.dingup.mock.enums.ExamContentTypeEnum;
import com.dingup.mock.module.SExaminationQueueCache;
import com.dingup.mock.util.ConstUtil;
import com.dingup.mock.util.SysUtil;
import com.suyeer.basic.util.BHttpResUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static com.dingup.api.base.BaseUtil.*;
import static com.dingup.mock.util.ConstUtil.MAPPER_PARAM_SUBJECT_NAME;


/**
 * Created by yanggavin on 16/5/4.
 */
@Controller
@RequestMapping("/writing")
public class WritingController {
    @Autowired
    private TsWritingQuestionsMapper tsWritingQuestionsMapper;

    @RequestMapping(value = "/get_data.do", method = RequestMethod.POST)
    public void getSpeakingAllData(@RequestParam(ConstUtil.REQUEST_PARAMETER_SUBJECT_NAME) String subjectName,
                                   HttpServletRequest request,
                                   HttpServletResponse response) {
        HttpResContent<WritingDataMapInfo> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取写作数据");
        try {
            // check param valid
            checkParam(ConstUtil.REQUEST_PARAMETER_SUBJECT_NAME, subjectName);
            // do
            WritingDataMapInfo wdmi = new WritingDataMapInfo();
            wdmi.setSubjectName(subjectName);
            HashMap<String, Object> param = new HashMap<>();
            param.put(MAPPER_PARAM_SUBJECT_NAME, subjectName);
            List<TsWritingQuestion> questions = tsWritingQuestionsMapper.getQuestions(param);
            for (TsWritingQuestion question : questions) {
                wdmi.getQuestionMap().put("q" + question.getQuestionNum(), question);
            }
            saveSuccessMsgAndData(hrc, wdmi);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc);
        }
    }

    @RequestMapping(value = "/save_answer.do", method = RequestMethod.POST)
    public void saveAnswer(@RequestParam(ConstUtil.REQUEST_PARAMETER_EXAMINATION_ID) String examinationId,
                           @RequestParam(ConstUtil.REQUEST_PARAMETER_SUBJECT_NAME) String subjectName,
                           @RequestParam(ConstUtil.REQUEST_PARAMETER_QUESTION_NUM) Integer questionNum,
                           @RequestParam(ConstUtil.REQUEST_PARAMETER_ANSWER) String answer,
                           HttpServletRequest request,
                           HttpServletResponse response) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("答案保存");
        try {
            // check param valid
            checkParam(ConstUtil.REQUEST_PARAMETER_EXAMINATION_ID, examinationId);
            checkParam(ConstUtil.REQUEST_PARAMETER_SUBJECT_NAME, subjectName);
            checkParam(ConstUtil.REQUEST_PARAMETER_QUESTION_NUM, questionNum.toString());
            // save answer.
            ExaminationInfo ei = SExaminationQueueCache.getInstance().getExaminationInfo(examinationId);
            ExamWritingInfo ewi = ei.getExamWritingInfo();
            if (ewi.getAnswerMap().containsKey(questionNum)) {
                ewi.getAnswerMap().put(questionNum, answer);
                // log answer
                SysUtil.logExamRecordInfo(ei.getUserId(), examinationId, ExamContentTypeEnum.WRITING, 0, questionNum, answer);
            }
            //// 每次更新答案, 都更新记录的最后访问时间, 以延长缓存时间
            ei.setLastUpdateTime(new Date());
            ei.setCurAnswerProcess(ExamContentTypeEnum.WRITING, String.format("q%s", questionNum));
            // 同步数据到MC
            SExaminationQueueCache.getInstance().syncDataToMC(examinationId, ei);
            // response.
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc);
        }
    }

}
